﻿<!DOCTYPE html>
<html >
    <head>
    </head>
    <body>
        <div id='log'></div>
    </body>
    <script src="js/json2.js" note='for IE6.0 ,old browser'></script> 
    <script src="js/jQuery-1.12.4.min.js" note='for IE6.0 ,new version can use JQuery 2.x'></script> 
    <script src="js/org.dbcloud.mongodb.MongoCollection.js"></script> 
    <script>
        try {
            function log(msg) {
                $("#log").append("<p>" + msg + "</p>");
            }
            var table = new org.dbcloud.mongodb.MongoCollection();
            
            log("clear table");
            table.deleteMany({});
            table.dropIndexes();

            log("insertOne");
            table.insertOne({name: 'dbcloud'});

            log("insertOne and set options");
            table.insertOne({name: 'validation'}, {validation: true});

            log("insertMany");
            var list = [];
            for (var i = 0; i < 10; i++) {
                list.push({name: 'dbcloud' + i, index: i});
            }
            table.insertMany(list);

            list = [];
            for (var i = 10; i < 20; i++) {
                list.push({name: 'dbcloud' + i, index: i});
            }
            table.insertMany(list, {ordered: true, validation: true});

            log("namespace:" + JSON.stringify(table.getNamespace()));
            var data = table.find();
            log("find all");
            log(JSON.stringify(data));

            log("record count:" + table.count());

            log("getDocumentClass:" + JSON.stringify(table.getDocumentClass()));
            log("getCodecRegistry:" + JSON.stringify(table.getCodecRegistry()));
            log("count:" + table.count({index: {'$exists': true}}));

            var data = table.find({'index': {'$gt': 5}});
            log("find index > 5");
            log(JSON.stringify(data));

            var data = table.find({'index': {'$gt': 5}}, {projection: {'_id': 0}});
            log("find index > 5 and exclude _id column");
            log(JSON.stringify(data));

            var data = table.find({'index': {'$gt': 5}}, {projection: {'_id': 0}}, 2, 10);
            log("find index > 5 and exclude _id column and skip 2 ,limit 10");
            log(JSON.stringify(data));

            var data = table.find({'index': {'$gt': 5}},{projection: {'_id': 0},sort:{index:-1},skip:1,limit:5});
            log("find index > 5 and exclude _id column and sort by index dec");
            log(JSON.stringify(data));

            var data = table.find({'index': {'$gt': 5}}, {projection:{'_id': 0},sort:{name:-1,index:1}}, 2, 10);
            log("find index > 5 and exclude _id column and sort by name dec, index asc and skip 2 ,limit 10");
            log(JSON.stringify(data));

            log('deleteOne which name is validation');
            table.deleteOne({name: 'validation'});

            log('deleteMany which index > 15');
            table.deleteMany({index: {'$gt': 15}});
            var data = table.find();
            log("find all");
            log(JSON.stringify(data));

            log("replaceOne");
            table.replaceOne({name: 'dbcloud'}, {name: 'replace', status: 0});

            log("replaceOne with options ");
            table.replaceOne({'index': {'$gt': 100}}, {name: 'replace', status: 1, index: 100}, {upsert: true});
            log("find all");
            var data = table.find();
            log(JSON.stringify(data));

            log("updateOne");
            table.updateOne({name: 'replace'}, {'$set': {name: 'update'}, '$inc': {status: 1}});

            log("updateOne with options ");
            table.updateOne({'index': {'$gt': 100}}, {'$set': {name: 'update', status: 1, index: 100}}, {upsert: true, validation: false});
            log("find all");
            var data = table.find();
            log(JSON.stringify(data));

            log("updateMany");
            table.updateMany({name: 'update'}, {'$set': {name: 'updateMany'}, '$inc': {status: 1}});

            log("updateMany with options ");
            table.updateMany({'index': {'$gt': 10}}, {'$set': {name: 'updateMany', status: 100, index: 100}}, {upsert: true, validation: false});
            log("find all");
            var data = table.find();
            log(JSON.stringify(data));

            log('findOneAndDelete');
            log(JSON.stringify(table.findOneAndDelete({'index': {'$gt': 10}})));

            log('findOneAndDelete with options');
            log(JSON.stringify(table.findOneAndDelete({'index': {'$gt': 10}}, {projection: {index: true, _id: false}, sort: {index: -1}})));

            log('findOneAndReplace');
            log(JSON.stringify(table.findOneAndReplace({'index': {'$gt': 10}}, {name: 'findOneAndReplace', index: 0})));

            log('findOneAndReplace with options');
            log(JSON.stringify(table.findOneAndReplace({'index': {'$gt': 10}}, {name: 'findOneAndReplace', index: 100}, {upsert: true, returnNew: true, projection: {index: true, _id: false}, sort: {index: -1}})));
            log("find all");
            var data = table.find();
            log(JSON.stringify(data));

            log('findOneAndUpdate');
            log(JSON.stringify(table.findOneAndUpdate({'index': {'$gt': 10}}, {'$set': {name: 'findOneAndUpdate'}, '$inc': {index: -1}})));

            log('findOneAndUpdate with options');
            log(JSON.stringify(table.findOneAndUpdate({'name': 'findOneAndUpdate'}, {'$set': {op: 'findOneAndUpdate'}, '$inc': {index: -10}}, {upsert: true, returnNew: true, projection: {index: true, name: true, op: true, _id: false}, sort: {index: -1}})));

            log('createIndex');
            var indexs = table.createIndex({index: -1});
            log(indexs);
            log('listIndexes: ');
            log(JSON.stringify(table.listIndexes()));

            table.dropIndex(indexs);
            table.dropIndexes();
            indexs = table.createIndex({index: -1}, {background: true, unique: false, name: 'myindexs'});
            log(JSON.stringify(indexs));

            table.dropIndexes();
            log('createIndexes:');
            log(JSON.stringify(table.createIndexes([{keys: {index: -1, name: 1}, options: {unique: false, name: 'nameindexs'}}, {keys: {index: -1, name: -1}, options: {unique: false, name: 'nameindexs2'}}])));

            log('listIndexes: ');
            log(JSON.stringify(table.listIndexes()));

            log('drop:');
            table.drop();
            table.close();
            
        } catch (e) {
            alert(e.message);
        }
    </script>
</html>
